#!/bin/bash

export NEW_HOSTNAME=`hostname`

DB_IP=`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
DB_IP=`echo $DB_IP | cut -d\  -f1`

sudo -- sh -c "echo $DB_IP  ${NEW_HOSTNAME} >>/etc/hosts"

. /migration/env.sh

sudo echo "Stop DAS and DB2"
/migration/stopDb2.sh

sudo echo "Discover new hostname"
UNAME_CACHE=${NEW_HOSTNAME} 
sudo echo "Using Hostname: " ${NEW_HOSTNAME}

if [ -e $INST_PATH/db2nodes.cfg ]; then
  sudo echo "Adjust db2nodes.cfg"
  sudo rm -f $INST_PATH/db2nodes.cfg.old
  sudo mv $INST_PATH/db2nodes.cfg $INST_PATH/db2nodes.cfg.old
  sudo touch $INST_PATH/db2nodes.cfg
  sudo chown db2inst1:db2iadm1 $INST_PATH/db2nodes.cfg
  sudo chmod uog+rw $INST_PATH/db2nodes.cfg
  sudo echo 0 $UNAME_CACHE 0 > $INST_PATH/db2nodes.cfg
fi

sudo echo "Make registry writable"
sudo chmod u+w /var/db2/*

sudo echo "Adjust the DB2 hostname"
sudo $DB2_PATH/adm/db2set -g db2system=$UNAME_CACHE

sudo echo "Update DAS config"
sudo $AS_DB2 "db2 update admin cfg using DB2SYSTEM $UNAME_CACHE"
sudo $AS_DB2 "db2 update admin cfg using SMTP_SERVER $UNAME_CACHE"

sudo echo "Start DB2 and DAS"
/migration/startDb2.sh
